<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：查重复管理控制器
 * 作者：吴志强
 * 创建日期：2018/5/25
 */
class CheckrepeatController extends CommonController {

    //查找重复的用户
    public function repeat(){


        $sex_enum = getenumbykey('sex');
        $sex_arr = getarrbyenum($sex_enum);
        $flag_enum = getenumbykey('flag');
        $flag_arr = getarrbyenum($flag_enum);
        $state_enum = getenumbykey('userlogin_state');
        $state_arr = getarrbyenum($state_enum);
        $sex = trim(I('ddlsex'));
        $flag = trim(I('ddlflag'));
        $appcode = trim(I('ddlapp'));
        $state = trim(I('ddlstate'));
        $keyword = trim(I('txtkeyword'));
        $app_code = $this->app_code;
        $login_type = $this->login_type;
        $map = array('_complex' => '1=1');

        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            }
        }
        if (!empty($sex)) {
            if ($sex == '3') {
//		$map['_complex'] = 'user_sex not in (\'1\',\'2\')';
                $map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
            } else {
                $map['user_sex'] = $sex;
            }
        }
        if (!empty($recom)) {
            $map['is_recommend'] = $recom;
        }
        if (!empty($active)) {
            $map['is_active'] = $active;
        }
        if (!empty($flag)) {
            if ($flag == '3') {
                $map['user_flag_switch'] = '2';
            } else {
                $map['user_flag_switch'] = '1';
                $map['user_flag'] = $flag;
            }
        }

        $user_uid="user_uid";
        $user_phone="user_phone";
        $order=$user_uid;
        $user_phone= $user_phone ? ",user_phone " : "user_phone";
        $field=$user_uid.$user_phone;
        $select_condition=M('chat_userinfo')->field($field)->group($field)->having('count(*)>1')->buildSql();
        $total =M('chat_userinfo')->where("($field)  in $select_condition")->order($order)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();

//            "select user_uid,user_phone from chat_userinfo group by user_uid,user_phone having count(*) > 1"
        $data=M('chat_userinfo')->where("($field)  in $select_condition")->order($order)->select();
        foreach ($data as $key => $value) {
            $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
            $data[$key]['user_flag_text'] = $data[$key]['user_auth'] == '2' ? ($data[$key]['user_flag_switch'] == '1' ? $flag_enum[$data[$key]['user_flag']] : '禁止上架') : '';
            $data[$key]['user_state_text'] = $state_enum[$data[$key]['is_lock']];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'statesel' => $state,  'sexsel' => $sex, 'recomsel' => $recom, 'activesel' => $active,  'flagsel' => $flag,  'keyword' => $keyword);
        $this->assign(array(
            'login_type' => $login_type,
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'statearr' => $state_arr,
            'sexarr' => $sex_arr,
            'flagarr' => $flag_arr,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }


    public function delrepeat(){
        $id=trim(I('id'));
        $map['id']=$id;
        $data=M('chat_userinfo')->where($map)->find();
        $user_phone=$data['user_phone'];
        $isok=M('chat_userinfo')->field('id')->where("user_phone = $user_phone and ((user_phone)  in (select user_phone from chat_userinfo group by user_phone having count(*) > 1))")->select();
        if($isok){
            M()->startTrans();//开启事务

            $user_uid=$data['user_uid'];
            $select_wallet_balance=M('chat_userwallet')->where("`user_uid` = '$user_uid' and `balance` NOT IN (select MAX(`balance`) from chat_userwallet group by `user_uid` having count(*)>1)")->getField('id');

            $select_account_id=M('chat_useraccount')->where("`account` = '$user_phone' and `ID` IN (select MIN(`ID`) from chat_useraccount group by `user_uid` having count(*)>1)")->getField('id');
            if(!$select_wallet_balance){
                $select_wallet_id=M('chat_userwallet')->where("`user_uid` = '$user_uid' and `ID`  IN (select MIN(`ID`) from chat_userwallet group by `user_uid` having count(*)>1)")->getField('id');
            }
            $a=0;
//            if($a==1){
                if($select_wallet_balance){
                    $wallet=M('chat_userwallet')->where('id ='.$select_wallet_balance)->delete();
                    $b="用户有相应多余钱包记录存在，已删除";
                }elseif ($select_wallet_id){
                    $wallet=M('chat_userwallet')->where('id ='.$select_wallet_id)->delete();
                    $b="用户有相应多余钱包记录存在，已删除";
                }
                if($select_account_id){
                    $account=M("chat_useraccount")->where('id ='.$select_account_id)->delete();
                    $c="用户有相应注册记录存在，已删除";
                    if(!$account){
                        //操作失败后
                        M()->rollback();
                        return $this->ajaxReturn(array('ret' => '1002', 'code' =>  '失败！已回滚数据'));
                    }
                }
                $info=M('chat_userinfo')->where($map)->delete();
                $a="有相应用户记录存在，已删除";
                if(($select_wallet_balance || $select_wallet_id) && !$wallet){
                    //操作失败后
                    M()->rollback();
                    return $this->ajaxReturn(array('ret' => '1002', 'code' =>  '失败！已回滚数据'));
                }
                if(!$info){
                    //操作失败后
                    M()->rollback();
                    return $this->ajaxReturn(array('ret' => '1002', 'code' =>  '失败！已回滚数据'));
                }

//            if()
            //操作成功后
            M()->commit();
//                 M()->rollback();
            return $this->ajaxReturn(array('ret' => '1001', 'code' =>  '成功！'.$a.$b.$c));
            
//            }
        }
        //操作失败后
        M()->rollback();
        return $this->ajaxReturn(array('ret' => '1002', 'code' =>  '该数据不是重复数据！'));

    }
}